import artical from '../../api/artical'
import * as types from '../mutation-types.js'

const state = {
  tableData: [],
  result: [],
  notice: [],
  detail: [],
  count: 0,
  editDetail: {}
}

const getters = {
  allTableDatas: state => state.tableData,
  allResult: state => state.result,
  allNotice: state => state.notice,
  detailArtical: state => state.detail,
  articalCount: state => state.count,
  editDetail: state => state.editDetail
}

const actions = {
  getTableDatas ({ commit }, { page }) {
    return new Promise((resolve, reject) => {
      artical.getData(page)
        .then(data => {
          if (window.localStorage.root === '1') {
            commit(types.GET_TABLEDATA, data.data.data)
            commit(types.COUNT, data.count)
            resolve()
          } else {
            commit(types.GET_TABLEDATA, data.data.data.news)
            commit(types.COUNT, data.count)
            resolve()
          }
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  },
  updateNews ({commit, dispatch}, params) {
    return new Promise((resolve, reject) => {
      artical.editNews(params)
        .then(() => {
          commit(types.NOTICE, '更新成功')
          dispatch('getTableDatas', {page: 1})
          resolve()
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  },
  getNewsDetail ({commit}, {newsId}) {
    return new Promise((resolve, reject) => {
      artical.getDetail(newsId)
        .then(data => {
          commit(types.GET_NEWSDETAIL, data.data)
          resolve()
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  },
  deleteData ({ commit, dispatch }, { newsId }) {
    return new Promise((resolve, reject) => {
      artical.deleteData(newsId)
        .then(data => {
          commit(types.NOTICE, data)
          dispatch('getTableDatas', {page: 1})
          resolve()
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  },
  review ({ commit }, { newsId }) {
    return new Promise((resolve, reject) => {
      artical.review(newsId)
        .then(data => {
          commit(types.DETAIL, data)
          resolve()
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  },
  examine ({commit, dispatch}, {params}) {
    return new Promise((resolve, reject) => {
      artical.examine(params)
        .then(data => {
          commit(types.NOTICE, '审核成功')
          dispatch('getTableDatas', {page: 1})
          resolve()
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  },
  getSearchData ({commit}, params) {
    return new Promise((resolve, reject) => {
      artical.searchData(params)
        .then(data => {
          commit(types.GET_TABLEDATA, data.data.data)
          commit(types.COUNT, data.count)
          resolve()
        })
        .catch(err => {
          commit(types.ERR_ARTICAL, err)
        })
    })
  }
}

const mutations = {
  [types.GET_TABLEDATA] (state, data) {
    state.tableData = data
  },
  [types.ERR_ARTICAL] (state, err) {
    state.result.push({ msg: err })
  },
  [types.NOTICE] (state, data) {
    state.notice.push({ msg: data })
  },
  [types.DETAIL] (state, data) {
    state.detail.push(data)
  },
  [types.COUNT] (state, data) {
    state.count = data
  },
  [types.GET_NEWSDETAIL] (state, data) {
    state.editDetail = data
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
